package com.qms.repast.ui.presenter;

import android.app.Activity;
import android.database.Cursor;

import com.qms.repast.App;
import com.qms.repast.entity.resbean.LablesBean;
import com.qms.repast.ui.base.BasePresenter;
import com.qms.repast.ui.view.ISearchView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 泅渡者
 * @date 2018/9/26 0020
 * @describe:
 */
public class SearchPresenter extends BasePresenter<ISearchView> {

	public SearchPresenter (Activity mContext, ISearchView mView) {
		super(mContext, mView);
	}

	/**
	 * 查找历史搜索记录并去重
	 */
	public void selectHistory () {
		String sql = "SELECT DISTINCT LABLE_NAME , LABLE_TYPE FROM LABLES_BEAN ORDER BY _id DESC";
		List<LablesBean> beans = new ArrayList<>();
		Cursor cursor = App.getmDaoSession().getDatabase().rawQuery(sql, null);
		if (cursor.moveToFirst()) {
			do {
				LablesBean lablesBean = new LablesBean();
				lablesBean.setLableName(cursor.getString(cursor.getColumnIndex("LABLE_NAME")));
				lablesBean.setLableType(cursor.getString(cursor.getColumnIndex("LABLE_TYPE")));
				beans.add(lablesBean);
			}
			while (cursor.moveToNext());
		}

		cursor.close();
		mView.updateHistory(beans);
	}

	/**
	 * 清空搜索记录
	 */
	public void deleteHistory () {
		App.getmDaoSession().getLablesBeanDao().deleteAll();
		mView.updateHistory(new ArrayList<LablesBean>());
	}
}
